﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_priceset_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_face_m_price_type(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetPriceType(int.Parse(this.hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    this.ddlComList.Items.FindByValue(dt.Rows[0]["self_community_code"].ToString()).Selected = true;
                    this.txtPriceName.Text = dt.Rows[0]["price_name"].ToString();
                    this.ddlPriceType.Items.FindByValue(dt.Rows[0]["price_type"].ToString()).Selected = true;
                    this.ddlPriceRule.Items.FindByValue(dt.Rows[0]["price_rule"].ToString()).Selected = true;
                    this.txtMachineType.Text = dt.Rows[0]["machinetype"].ToString();
                    this.txtBeginDate.Text = dt.Rows[0]["begin_date"].ToString();
                    this.txtEndDate.Text = dt.Rows[0]["end_date"].ToString();

                }
            }
        }


        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sCommunity = this.ddlComList.SelectedValue;
            string sPriceName = this.txtPriceName.Text.Trim();
            string sPriceType = this.ddlPriceType.SelectedValue;
            string sPriceRule = this.ddlPriceRule.SelectedValue;
            string sMachineType = this.txtMachineType.Text.Trim();
            string sDD = this.txtBeginDate.Text.Trim();
            string eDD = this.txtEndDate.Text.Trim();
            DateTime sd = DateTime.Now;
            DateTime ed = DateTime.MaxValue;
            if (sCommunity == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "社区不能为空！");
                return;
            }
            if (sPriceName == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "费项名称不能为空！");
                return;
            }
            if (sDD != string.Empty)
            {
                if (!DateTime.TryParse(sDD, out sd)) { PM.Web.Common.JsAlert(this, "开始日期格式不正确！"); return; }
            }
            if (eDD != string.Empty)
            {
                if (!DateTime.TryParse(eDD, out ed)) { PM.Web.Common.JsAlert(this, "结束日期格式不正确！"); return; }
            }
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_price_type(int.Parse(sCommunity), sPriceName, sPriceType, sMachineType, false, sPriceRule, sd, ed))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！",true);
                }
            }
            else
            {
                if (dal.Modify_face_m_price_type(int.Parse(this.hidId.Value), int.Parse(sCommunity), sPriceName, sPriceType, sMachineType, false, sPriceRule, sd, ed))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！",true);
                }
            }
        }
    }
}